package com.example.bishe.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bishe.entity.Approval;
import com.example.bishe.entity.UserRoom;
import com.example.bishe.mapper.ApprovalMapper;
import com.example.bishe.service.ApprovalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bishe.service.UserRoomService;
import com.example.bishe.web.b.vo.getApprovalPageVO;
import com.example.bishe.web.c.vo.UserApprovalVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LDH
 * @since 2022/11/30
 */
@Service
public class ApprovalServiceImpl extends ServiceImpl<ApprovalMapper, Approval> implements ApprovalService {

    @Autowired
    private ApprovalMapper approvalMapper;
    @Autowired
    private UserRoomService userRoomService;

    /**
    * 功能描述: 根据用户id获取用户所创建空间的加入申请
    * @Param: [userId]
    * @return: java.util.List<com.example.bishe.web.c.vo.UserApprovalVO>
    * @Author: LDH
    * @Date: 2023/04/23
    */
    @Override
    public IPage<UserApprovalVO> getUserApproval(int pageNum, int pageSize, Long userId) {
        IPage<UserApprovalVO> page = new Page<>(pageNum, pageSize);
        return approvalMapper.getUserApproval(page, userId);
    }

    /**
    * 功能描述: 分页查询审批
    * @Param: [pageNum, pageSize, id, userId, userName, result]
    * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.example.bishe.web.b.vo.getApprovalPageVO>
    * @Author: LDH
    * @Date: 2023/05/08
    */
    @Override
    public IPage<getApprovalPageVO> getApprovalPage(int pageNum, int pageSize, Long id, Long userId, String userName, Integer result, String startTime, String endTime) {
        Page<getApprovalPageVO> page = new Page<>(pageNum,pageSize);
        return baseMapper.getPage(page,id,userId,userName,result,startTime,endTime);
    }

    /**
    * 功能描述: 管理员审批]
    * @Param: [approval]
    * @return: void
    * @Author: LDH
    * @Date: 2023/05/08
    */
    @Override
    public void update(Approval approval) {
        baseMapper.updateById(approval);
    }

    /**
    * 功能描述: 用户同意或拒绝加入
    * @Param: [approval]
    * @return: void
    * @Author: LDH
    * @Date: 2023/05/12
    */
    @Override
    public void doApply(Approval approval) {
        if (approval.getResult() == 1){
            UserRoom userRoom = new UserRoom();
            userRoom.setRoomId(approval.getRoomId());
            userRoom.setUserId(approval.getUserId());
            userRoomService.save(userRoom);
        }

        baseMapper.updateById(approval);
    }

    /**
    * 功能描述: 删除单个审批数据
    * @Param: [approval]
    * @return: void
    * @Author: LDH
    * @Date: 2023/05/08
    */
    @Override
    public void delOne(Approval approval) {
        baseMapper.deleteById(approval);
    }

    /**
    * 功能描述: 批量删除审批数据
    * @Param: [approvals]
    * @return: void
    * @Author: LDH
    * @Date: 2023/05/08
    */
    @Override
    public void delBatch(List<Approval> approvals) {
        List<Long> list = approvals.stream().map(Approval::getId).collect(Collectors.toList());
        baseMapper.deleteBatchIds(list);
    }
}
